<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/25 15:31
 */

namespace app\admin\business;


use app\common\lib\Arr;
use app\common\model\mysql\Role as RoleModel;
use app\common\model\mysql\Auth as AuthModel;

class Role extends AdminBase
{
    /**
     * 功能说明 添加角色数据
     * @author: wang
     * @Date: 2020/3/25 15:54
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public static function add($data)
    {
        $RoleModel = new RoleModel();
        try {
            $res = $RoleModel->save($data);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('添加了【' . $data['name'] . '】角色信息');
        return $res;
    }

    /**
     * 功能说明 更新数据
     * @author: wang
     * @Date: 2020/3/25 18:24
     * @param $data
     * @return mixed
     * @throws \think\Exception
     */
    public static function edit($data)
    {
        $RoleModel = new RoleModel();
        try {
            $res = $RoleModel->edit($data);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('更新了【' . $data['name'] . '】角色信息');
        return $res;
    }

    /**
     * 功能说明 获取角色列表
     * @author: wang
     * @Date: 2020/3/25 18:02
     * @param string $limit
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function getRoleList($where, $limit = '15')
    {
        $RoleModel = new RoleModel();
        return $RoleModel->getRoleList($where, $limit)->toArray();
    }

    /**
     * 功能说明 通过ID获取数据
     * @author: wang
     * @Date: 2020/3/25 20:50
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDataById($id)
    {
        $RoleModel = new RoleModel();
        return $RoleModel->getDataById($id);
    }

    /**
     * 功能说明 更新状态
     * @author: wang
     * @Date: 2020/3/25 17:56
     * @param $data
     * @return mixed
     */
    public static function upStatus($data)
    {
        $RoleModel = new RoleModel();
        //记录日志
        $RoleModel->insert_admin_log('更新了ID为-【' . $data['id'] . '】的角色状态');
        return $RoleModel->upStatus($data);
    }

    /**
     * 功能说明 通过ID删除数据
     * @author: wang
     * @Date: 2020/3/25 20:54
     * @param $id
     * @return bool
     * @throws \think\Exception
     */
    public static function delDataById($id)
    {
        if($id == 1){
            throw new \think\Exception('无法删除超级管理员');
        }
        $RoleModel = new RoleModel();
        try {
            $res = $RoleModel->delDataById($id);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('删除了ID为-【' . $id . '】的角色信息');
        return $res;
    }

    /**
     * 功能说明 通过ID批量删除数据
     * @author: wang
     * @Date: 2020/3/27 11:55
     * @param $ids
     * @return bool
     * @throws \think\Exception
     */
    public static function delDataByIds($ids)
    {
        if (empty($ids)) {
            throw new \think\Exception('未选择数据');
        }
        $idArr = explode(',', $ids);
        $key = array_search(1, $idArr);
        if ($key !== false){
            array_splice($idArr, $key, 1);
        }
        $RoleModel = new RoleModel();
        try {
            $res = $RoleModel->delDataById($idArr);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('批量删除了ID为-【' . $ids . '】的角色信息');
        return $res;
    }

    /**
     * 功能说明 获取超级管理员外的所有角色
     * @author: wang
     * @Date: 2020/3/25 20:59
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllRole()
    {
        $RoleModel = new RoleModel();
        $where[] = ['id', '>', 1];
        return $RoleModel->getAllRole($where)->toArray();
    }

    /**
     * 功能说明 获取权限节点
     * @author: wang
     * @Date: 2020/3/26 21:27
     * @param $id
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRoleAuth($id)
    {
        if (empty($id)) {
            throw new \think\Exception('ID错误');
        }
        $RoleModel = new RoleModel();
        $myAuth = $RoleModel->getDataById($id)->toArray();
        $rules = [];
        if (!empty($myAuth['rules'])) {
            $rules = explode(',', $myAuth['rules']);
        }

        $AuthModel = new AuthModel();
        $res = $AuthModel->order('sort asc')->select()->toArray();
        $nav = new Arr();
        $data = $nav->rule($res);
        $res = $nav->arrToAuthArr($data, $rules);

        return $res;

    }

    /**
     * 功能说明 设置角色权限
     * @author: wang
     * @Date: 2020/3/27 12:06
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public static function setRoleAuth($data)
    {
        $id = $data['id'];
        if (empty($id)) {
            throw new \think\Exception('ID错误');
        }
        $arr['id'] = $id;
        $ids = '';
        foreach ($data as $k => $v) {
            if((strpos($k, 'layuiTreeCheck_')) === 0 ){
                $ids .= $v . ',';
            }
        }
        $arr['ids'] = trim($ids, ',');

        $RoleModel = new RoleModel();
        try {
            $res = $RoleModel->setRoleAuth($arr);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('设置了ID为-【' . $arr['id'] . '】的权限为:【'.$arr['ids'].'】');
        return $res;
    }

}